package com.lanou3g.zjl.chating.home.friends;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.home.personal_center.CircleImageView;
import com.lanou3g.zjl.chating.utils.GetUserAvatarHelper;
import com.lanou3g.zjl.chating.volley.VolleySingleton;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/11/19.
 */
public class FriendsListAdapter extends BaseAdapter {

    private List<FriendsInfo> friendsList = null;//存储好友的集合
    private Context mContext;

    //构造方法初始化数据
    public FriendsListAdapter(Context context, List<FriendsInfo> data) {
        this.mContext = context;
        friendsList = data;
    }

    @Override
    public int getCount() {

        return friendsList != null && friendsList.size() > 0 ? friendsList.size() : 0;
    }

    @Override
    public FriendsInfo getItem(int position) {

        return friendsList != null && friendsList.size() > 0 ? friendsList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        final FriendsInfo info = friendsList.get(position);
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.friends_item_adapter, null);

            holder = new ViewHolder(view);

            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }
        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        // 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            holder.letter.setVisibility(View.VISIBLE);
            holder.letter.setText(info.getSortLetter());
        } else {
            holder.letter.setVisibility(View.GONE);
        }
        holder.friendName.setText(info.getName());
        GetUserAvatarHelper helper = new GetUserAvatarHelper(mContext,holder.avatar,info.getName());
        helper.execute(info.getName());
        return view;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        //放回第0个位置的char值
        return friendsList.get(position).getSortLetter().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = friendsList.get(i).getSortLetter();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<FriendsInfo> list) {
        this.friendsList = list;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @Bind(R.id.head_img)
        CircleImageView avatar;//头像
        @Bind(R.id.friendName)
        TextView friendName;//好友名
        @Bind(R.id.catalog)
        TextView letter;//首字母

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
